using UnityEngine;

namespace Framework
{
    public class PhysicCurve
    {
        public float Duration { get; private set; }
        private Vector3 _startPosition;
        private float _g;
        private float _v;
        private Vector3 _forward;
        public PhysicCurve(float g)
        {
            _g = g;
        }
        public void Set(Vector3 startPosition, Vector3 endPosition, float height)
        {
            _startPosition = startPosition;
            var h0 = startPosition.y;
            var h1 = endPosition.y;
            var hh = Mathf.Max(h0, h1) + Mathf.Abs(height);
            var s0 = hh - h0;
            var s1 = hh - h1;
            var g = Mathf.Abs(_g);
            var t0 = Mathf.Sqrt(s0 / (0.5f * g));
            var t1 = Mathf.Sqrt(s1 / (0.5f * g));
            Duration = t0 + t1;
            _v = g * t0;
            startPosition.y = 0;
            endPosition.y = 0;
            _forward = (endPosition - startPosition);
        }
        public Vector3 Time(float value)
        {
            var point = _forward * (value / Duration);
            point.y = _v * value + 0.5f * _g * value * value;
            return _startPosition + point;
        }
        public Vector3 Progress(float value)
        {
            return Time(Duration * value);
        }
    }
}